package boot

import (
	"dc-go/config"
	"dc-go/global"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func Postgres() *gorm.DB {
	dcPostgresConfig := global.DcConfig.Postgres
	if dcPostgresConfig.Database == "" {
		return nil
	}
	postgresConfig := config.PostgresConfig(dcPostgresConfig)
	gormConfig := config.GormConfig(dcPostgresConfig)
	if db, err := gorm.Open(postgres.New(postgresConfig), &gormConfig); err != nil {
		return nil
	} else {
		postgresDb, _ := db.DB()
		postgresDb.SetMaxIdleConns(dcPostgresConfig.MaxIdle)
		postgresDb.SetMaxOpenConns(dcPostgresConfig.MaxOpen)
		return db
	}
}
